<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\facade\Db;
use think\facade\View;

class Index extends Base{
    public function index(){
        return view();
    }

    // 修改后台入口文件名
    public function editadminfile(){
        if(!request()->isPost()){
            return view();
        }else{
            // 转为小写字母
            $general = new \common\General();
            $filename = $general->capital_to_underline(trim(input('filename')));

            if(empty($filename) or in_array($filename,array('index','admin','api')) or !preg_match('/^[a-z]{0,20}$/',$filename)){
                $filename = str_replace('.php','',$filename);
                return json(['code' => 1001, 'msg' => lang('v_document_name').lang('g_illegal'), 'data' => $filename]);
            }
            $rootPath = request()->server('DOCUMENT_ROOT').'/';
            if(is_file($rootPath.'admin.php')){
                $result = rename($rootPath.'admin.php',$rootPath.$filename.'.php');
                if($result){
                    // 修改成功，退出重新登录
                    session(null);
                    return json(['code' => 2000, 'msg' => lang('c_success'), 'data' => $filename.'.php']);
                }
            }
            return json(['code' => 1002, 'msg' => lang('c_fail'), 'data' => $filename]);
        }
    }
	
	// 清理缓存
    public function delCache(){
        $delete_path = app()->getRootPath().'runtime'.DS;
        $cannot_delete_path = $delete_path.'session'.DS;

        try {
            $dofile = new \common\Dofile();
            $dofile->delDirAndFile($delete_path,true,array($cannot_delete_path));
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => '']);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
}